/*
    This file is part of Epoch <http://code.google.com/p/epoch/>.

    Copyright (C) 2011 Adam Meily <meily.adam@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include "DatabaseConfig.h"

DatabaseConfig::DatabaseConfig()
{

}

DatabaseConfig::~DatabaseConfig()
{

}

QString DatabaseConfig::backupTable() const
{
    return m_tables["backup"];
}

const QString& DatabaseConfig::database() const
{
    return m_database;
}

QString DatabaseConfig::deviceTable() const
{
    return m_tables["device"];
}

const QString& DatabaseConfig::hostname() const
{
    return m_hostname;
}

const QString& DatabaseConfig::password() const
{
    return m_password;
}

quint16 DatabaseConfig::port() const
{
    return m_port;
}

QString DatabaseConfig::revisionTable() const
{
    return m_tables["revision"];
}

QString DatabaseConfig::shareTable() const
{
    return m_tables["share"];
}

const QMap< QString, QString >& DatabaseConfig::tables() const
{
    return m_tables;
}

const QString& DatabaseConfig::username() const
{
    return m_username;
}

DatabaseConfig* DatabaseConfig::parse(const QSettings& config, QString* error)
{
    //TODO error detection
    //TODO custom table names
    QString username = config.value("db/user", "chronos").toString();
    QString password = config.value("db/password", "chronos").toString();
    QString hostname = config.value("db/host", "localhost").toString();
    QString database = config.value("db/database", "chronos");
    
    bool ok;
    quint16 port = config.value("db/port", 3306).toUInt(&ok);
    if(!ok)
        port = 3306;
    
    DatabaseConfig *dc = new DatabaseConfig();
    dc->m_username = username;
    dc->m_hostname = hostname;
    dc->m_password = password;
    dc->m_port = port;
    dc->m_database = database;
    
    dc->m_tables["backup"] = "backup";
    dc->m_tables["revision"] = "revision";
    dc->m_tables["device"] = "device";
    dc->m_tables["share"] = "share";
    
    return dc;
}
